package com.star.wellconnect.appts;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.star.wellconnect.R;
import com.star.wellconnect.sqlite.AppPreferences;
import com.star.wellconnect.sqlite.Appointment;

public class ApptsListViewAdapter extends BaseExpandableListAdapter{
	private Context mContext;
	private Map<String, List<Appointment>> mApptsCollection;
	private List<String> mApptsDates;
	
	public ApptsListViewAdapter(Context context, List<String> apptsDate, 
			Map<String, List<Appointment>> apptsCollection){
		this.mContext = context;
		this.mApptsDates = apptsDate;
		this.mApptsCollection = apptsCollection;
	}

	@Override
	public Object getChild(int groupIndex, int childIndex) {
		return mApptsCollection.get(mApptsDates.get(groupIndex)).get(childIndex);
	}

	@Override
	public long getChildId(int groupIndex, int childIndex) {
		return childIndex;
	}

	@Override
	public View getChildView(int groupIndex, int childIndex, boolean isLastChild, View view,
			ViewGroup parent) {
		final Appointment appt = (Appointment) getChild(groupIndex, childIndex);
		LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		if(view == null)
			view = inflater.inflate(R.layout.listview_adapter_appt_content, null);
		
		((TextView) view.findViewById(R.id.time)).setText(appt.getTime(AppPreferences.AM_PM_TIME));
		((TextView) view.findViewById(R.id.name)).setText(appt.name);
		((TextView) view.findViewById(R.id.location)).setText(appt.address);
		
		return view;
	}

	@Override
	public int getChildrenCount(int groupIndex) {
		return mApptsCollection.get(mApptsDates.get(groupIndex)).size();
	}

	@Override
	public Object getGroup(int groupIndex) {
		return mApptsDates.get(groupIndex);
	}

	@Override
	public int getGroupCount() {
		return mApptsDates.size();
	}

	@Override
	public long getGroupId(int groupIndex) {
		return groupIndex;
	}

	@Override
	public View getGroupView(int groupIndex, boolean isExpanded, View view, ViewGroup parent) {
        String date = (String) getGroup(groupIndex);
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.listview_adapter_appt_header, null);
        }
        ((TextView) view.findViewById(R.id.date)).setText(date);
        return view;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupIndex, int childIndex) {
		return true;
	}

}
